﻿@{
    ViewBag.Title = "客户编辑";
    var salerList = ViewBag.SalerList as IEnumerable<CubicFWNet.Data.Models.User>;
    var customer = ViewBag.Customer as CubicFWNet.Data.Models.Customer;
}

<section class="content-header">
    <h1>
        客户编辑
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active">客户编辑</li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">
                <div class="row">

                    <input type="hidden" name="CustomerID" value="@customer.Id" />
                    <input type="hidden" id="modalData" value="@customer.Principal">
                    <input type="hidden" id="Data" name="Data" />

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Name" class="col-sm-3 control-label">客户名称</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Name" name="Name" value="@customer.Name" maxlength="100">
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ShortName" class="col-sm-3 control-label">客户简称</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="ShortName" name="ShortName" value="@customer.ShortName" maxlength="50">
                        </div>
                    </div>
                   
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Creator" class="col-sm-3 control-label">创建人</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Creator" name="Tolerance" value="@ViewBag.RealName" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CreateTime" class="col-sm-3 control-label">创建时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CreateTime" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm")" readonly="readonly" />
                        </div>
                    </div>
                </div>


                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label class="col-sm-3 control-label">状态</label>
                        <div class="col-sm-9">
                            <span class="form-control" style="border:none">
                                @if (customer.Status == 1)
                                {
                                    <input type="radio" id="rbStatus1" name="Status" value="1" checked />
                                    <label for="rbStatus1">正常</label>
                                    <input type="radio" id="rbStatus2" name="Status" value="-1" style="margin-left:10px" />
                                    <label for="rbStatus2">禁用</label>
                                }
                                else
                                {
                                    <input type="radio" id="rbStatus1" name="Status" value="1" />
                                    <label for="rbStatus1">正常</label>
                                    <input type="radio" id="rbStatus2" name="Status" value="-1" checked style="margin-left:10px" />
                                    <label for="rbStatus2">禁用</label>

                                }
                            </span>
                        </div>
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <input type="hidden" name="SalerName" id="SalerName" />
                        <label for="Saler" class="col-sm-3 control-label">我方销售员</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="Saler" name="Saler">
                                @if (null != salerList)
                                {
                                    foreach (var item in salerList)
                                    {
                                        if (item.Id == customer.UserId)
                                        {
                                            <option value="@item.Id" selected>@item.RealName</option>
                                        }
                                        else
                                        {
                                        <option value="@item.Id">@item.RealName</option>
                                        }
                                    }
                                }
                            </select>
                        </div>
                    </div>
                </div>


          </div>



            <div class="box-footer">
                <div class="box-footer">
                    <span class="btn btn-primary " onclick="FormSub()">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
                    <a class="btn btn-primary" href="@Url.Action("CusList","SalerDirector")">返 回</a>
                </div>
            </div>
        </div>
    </form>
</section>

@section Scripts {
    <script>

        $(document).ready(function () {
            //解析Data数据
            var data = JSON.parse(document.getElementById("modalData").value);
            var $content = $('#json-content');
            var html = "";
            data.forEach(function (e, i) {
                if (typeof e.value == "undefined") {
                    e.value = "";
                }
                if (typeof e.name == "undefined") {
                    e.name = "";
                }
                if (typeof e.des == "undefined") {
                    e.des = "未知";
                }
                html += "<div class='form-group col-md-6'>";
                html += "<label for='j_a' class='col-sm-3 control-label'>" + e.des + "</label>";
                html += "<div class='col-sm-9'>";
                html += "<input type='text' readonly class='form-control' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' >";
                html += "</div></div>";
            });
            $content.append(html);
        })

        //获取Json数据
        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('input,select,textarea').each(function (r) {
                var $this = $(this);
                var id = $this.attr('id');
                var type = $this.attr('type');
                if (id) {
                    switch (type) {
                        case "checkbox":
                            postdata.push({ des: $this.attr("data-des"), name: id, value: $this.is(":checked") });
                            break;
                        case "select":
                            var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                            postdata.push({ des: $this.attr("data-des"), name: id, value: value });
                            break;
                        default:
                            var value = $this.val() == "" ? "&nbsp;" : $this.val();
                            postdata.push({ des: $this.attr("data-des"), name: id, value: value });
                            break;
                    }
                }
            });
            return JSON.stringify(postdata);
        }

        //提交表单
        function FormSub() {

            var sal = $("#Saler").val();
            if (null == sal || sal == "") {
                $.modalAlert("请选择我方销售员", "warning");
                return false;
            }

            $("#SalerName").val($("#Saler").find("option:selected").text());

            //var data = getParam();
            //$("#Data").val(data);
            $("#formMain").submit();
        }

        //禁用input
        $("input").prop("readonly", "readonly");
    </script>
}